﻿/*
 * copyright (c) 2009 hvbird
 *
 * This file is part of tdlib.
 *
 * tdlib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * tdlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;

namespace TDLib
{
    public class Validate
    {

        /// <summary>
        ///  验证类
        /// </summary>
        public Validate() { }

        #region 验证属性
        private const string mChar = "^[A-Za-z]+$";
        private const string mCharInt = @"^\w+$";
        private const string mChineseWord = @"[\u4e00-\u9fa5]";
        private const string mColor = "^#[a-fA-F0-9]{6}";
        private const string mCompareProduct = @"^\d+(,\d+)*$";
        private const string mDate = @"^(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\/|-|\.)(?:0?2\1(?:29))$)|(?:(?:1[6-9]|[2-9]\d)?\d{2})(\/|-|\.)(?:(?:(?:0?[13578]|1[02])\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\2(?:0?[1-9]|1\d|2[0-8]))$";
        private const string mDoubleWord = @"[\u4e00-\u9fa5]";
        private const string mEmail = @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$";
        private const string mHTML = @"<(.*)>.*<\/\1>|<(.*) \/>";
        private const string mID = "d{18}|d{15}";
        private const string mInt = "^[0-9]*[1-9][0-9]*$";
        private const string mIp = @"^\(([0-1]\d{0,2})|(2[0-5]{0,2}))\.(([0-1]\d{0,2})|(2[0-5]{0,2}))\.(([0-1]\d{0,2})|(2[0-5]{0,2}))\.(([0-1]\d{0,2})|(2[0-5]{0,2}))$";
        private const string mLowCase = "^[a-z]+$";
        private const string mMobliePhone = "^0{0,1}13[0-9]{9}$";
        private const string mNoChar = @"([\u4e00-\u9fa5]\w)+";
        private const string mNum = @"^[+-]?[0-9]+(\.[0-9]+)?$";
        private const string mPath = @"^[a-zA-Z]{1}[:](\w&(\/|\\))*\w*";
        private const string mPhone = "((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*";
        private const string mPic = @"(.*)\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$";
        private const string mTime = "^(([0-1]?[0-9]?)|([2]?[0-3]?)):([0-5]?[0-9]?):([0-5]?[0-9]?)$";
        private const string mUpCase = "^[A-Z]+$";
        private const string mUrl = @"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
        private const string mUserName = @"\w*[\u4e00-\u9fa5]*";
        private const string mZip = @"(.*)\.(rar|zip|7zip|tgz)$";
        private const string mZipCode = "d{6}";
        private const string mCard = @"^(\d{15}|\d{18}|\d{17}[X|x])$";
        #endregion

        private static Regex Reg;

        /// <summary>
        /// 主验证函数
        /// </summary>
        /// <param name="redString"></param>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool RegData(string redString, string mString)
        {
            Validate.Reg = new Regex(redString);
            if (Validate.Reg.IsMatch(mString))
            {
                return true;
            }
            return false;
        }

        #region 验证函数
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsChar(string mString)
        {
            return Validate.RegData(mChar, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsCharAndNum(string mString)
        {
            return Validate.RegData(mNoChar, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsCharInt(string mString)
        {
            return Validate.RegData(mCharInt, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsChineseWord(string mString)
        {
            return Validate.RegData(mChineseWord, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsColor(string mString)
        {
            return Validate.RegData(mColor, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsCompareIDs(string mString)
        {
            return Validate.RegData(mCompareProduct, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsDate(string mString)
        {
            return Validate.RegData(mDate, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsDoubleWord(string mString)
        {
            return Validate.RegData(mDoubleWord, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsEmail(string mString)
        {
            return Validate.RegData(mEmail, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsHTML(string mString)
        {
            return Validate.RegData(mHTML, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsID(string mString)
        {
            return Validate.RegData(mID, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsInt(string mString)
        {
            return Validate.RegData(mInt, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsIP(string mString)
        {
            return Validate.RegData(mIp, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsLowCase(string mString)
        {
            return Validate.RegData(mLowCase, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsMobliePhone(string mString)
        {
            return Validate.RegData(mMobliePhone, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsNum(string mString)
        {
            return Validate.RegData(mNum, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsPath(string mString)
        {
            return Validate.RegData(mPath, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsPhone(string mString)
        {
            return Validate.RegData(mPhone, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsPic(string mString)
        {
            return Validate.RegData(mPic, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsTime(string mString)
        {
            return Validate.RegData(mTime, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsUpCase(string mString)
        {
            return Validate.RegData(mUpCase, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsUrl(string mString)
        {
            return Validate.RegData(mUrl, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsUserName(string mString)
        {
            return Validate.RegData(mUserName, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsZipCode(string mString)
        {
            return Validate.RegData(mZipCode, mString);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsZipFile(string mString)
        {
            return Validate.RegData(mZip, mString);
        }

        /// <summary>
        ///  身份证验证(15或18位) 
        /// </summary>
        /// <param name="mString"></param>
        /// <returns></returns>
        public static bool IsIdentityCard(string mString)
        {
            return Validate.RegData(mCard, mString);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlInput"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static string SqlText(string sqlInput, int maxLength)
        {
            if ((sqlInput != null) && (sqlInput != string.Empty))
            {
                sqlInput = sqlInput.Trim();
                if (sqlInput.Length > maxLength)
                {
                    sqlInput = sqlInput.Substring(0, maxLength);
                }
            }
            return sqlInput;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool UnSaveChar(string str)
        {
            string text1 = "~!@#$%^&*\uffe5|";
            foreach (char ch1 in str)
            {
                if (text1.IndexOf(ch1) >= 0)
                {
                    return false;
                }
            }
            return true;
        }
        #endregion
    }
}
